package cn.com.kblog.util;



import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author KK.Luo
 * Email luokai8@vip.qq.com
 * @date 2013-12-2下午11:33:53
 * 
 */
public class HttpUtils {
    private static final String USER_AGENT = "User-Agent";

    /**
     * 获取浏览器类型
     * 
     * Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.18)
     * Gecko/20110614 Firefox/3.6.18 Mozilla/5.0 (Linux; U; Android 2.3.4;
     * zh-cn; ME722 Build/4.5.3-109_MS2-5) AppleWebKit/533.1 (KHTML, like Gecko)
     * Version/4.0 Mobile Safari/533.1 Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like
     * Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2
     * Mobile/8J2 Safari/6533.18.5 MQQBrowser/2.5 Mozilla/5.0 (iPad; U; CPU OS
     * 4_3_3 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko)
     * Mobile/8J2 Safari/7534.48.3 Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_1
     * like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko)
     * Version/5.0.2 Mobile/8G4 Safari/6533.18.5 IUC(U;iOS
     * 4.3.1;Zh-cn;320*480;)/UCWEB8.5.1.178/41/800 Mozilla/5.0 (iPad; U; CPU OS
     * 4_3 like Mac OS X; zh-cn Model:iPad2,2) AppleWebKit/533.17.9 (KHTML, like
     * Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5 JUC (Linux; U; 2.3.7;
     * zh-cn; MB525; 480*854) UCWEB7.9.4.145/139/800 Mozilla/5.0 (Linux; U;
     * Android 2.3.7; zh-cn; MB525 Build/MIUI) AppleWebKit/533.1 (KHTML, like
     * Gecko) Version/4.0 Mobile Safari/533.1 Mozilla/4.0 (compatible; MSIE 8.0;
     * Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR
     * 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C;
     * .NET4.0E) Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML,
     * like Gecko) Chrome/18.0.1025.162 Safari/535.19 android pad版 ?
     * 
     * @param request
     * @return
     */
    public static String getUserAgent(final HttpServletRequest request) {
	if (null == request) {
	    return "";
	}

	System.out.println("user_agent:" + request.getHeader(USER_AGENT));
	return request.getHeader(USER_AGENT);
    }

    /**
     * 判断是否手机浏览器
     * 
     * @param request
     * @return
     */
    public static boolean isMobileBrowser(final HttpServletRequest request) {
	if (null == request) {
	    return false;
	}

	return getUserAgent(request).indexOf("Firefox") > -1
		|| getUserAgent(request).indexOf("Android") > -1
		|| getUserAgent(request).indexOf("iPhone") > -1
		|| getUserAgent(request).indexOf("Phone") > -1;// windows phone
    }

    /**
     * 判断是否PC浏览器
     * 
     * @param reqeust
     * @return
     */
    public static boolean isPCBrowser(final HttpServletRequest request) {
	if (null == request) {
	    return false;
	}
	return (getUserAgent(request).indexOf("Windows") > -1 || getUserAgent(
		request).indexOf("iPad") > -1);
    }

    /**
     * Return a cookie.
     */
    public static Cookie getCookie(final HttpServletRequest request, String name) {
	if (null == request || null == name) {
	    return null;
	}
	Cookie[] cookies = request.getCookies();
	if (cookies == null) {
	    return null;
	}
	for (Cookie cookie : cookies) {
	    if (cookie.getName().equals(name)) {
		return cookie;
	    }
	}
	return null;
    }

    /**
     * 获取cookies中值
     * 
     * @param defaultValue
     *                默认值
     */
    public static String getCookieValue(final HttpServletRequest request,
	    String name, String defaultValue) {
	if (null == request || null == name) {
	    return null;
	}
	Cookie cookie = getCookie(request, name);
	return (cookie == null || cookie.getValue() == null) ? defaultValue
		: cookie.getValue();
    }

    /**
     * 获取cookies中值
     * 
     * @param request
     * @param name
     * @return
     */
    public static String getCookieValue(final HttpServletRequest request,
	    String name) {
	return getCookieValue(request, name, null);
    }

    /**
     * Add Cookie to response.
     * 
     * @param name
     *                cookie name
     * @param value
     *                cookie value
     * @param maxAgeInSeconds
     *                -1: clear cookie when close browser. 0: clear cookie
     *                immediately. n>0 : max age in n seconds.
     * @param path
     *                see Cookie.setPath(String)
     * @param domain
     *                the domain name within which this cookie is visible; form
     *                is according to RFC 2109
     */
    public static void addCookie(final HttpServletResponse res, String name,
	    String value, int maxAgeInSeconds, String path, String domain) {
	if (null == res || null == name) {
	    return;
	}

	Cookie cookie = new Cookie(name, value);
	cookie.setMaxAge(maxAgeInSeconds);
	cookie.setPath(path);
	res.addCookie(cookie);
	cookie.setDomain(domain);
    }

    /**
     * Add Cookie to response.
     * 
     * @param name
     *                cookie name
     * @param value
     *                cookie value
     * @param maxAgeInSeconds
     *                -1: clear cookie when close browser. 0: clear cookie
     *                immediately. n>0 : max age in n seconds.
     * @param path
     *                see Cookie.setPath(String)
     */
    public static void addCookie(final HttpServletResponse res, String name,
	    String value, int maxAgeInSeconds, String path) {
	if (null == res || null == name) {
	    return;
	}
	Cookie cookie = new Cookie(name, value);
	cookie.setMaxAge(maxAgeInSeconds);
	cookie.setPath(path);
	res.addCookie(cookie);
    }

    /**
     * Add Cookie with path = "/".
     */
    public static void addCookie(HttpServletResponse res, String name,
	    String value, int maxAgeInSeconds) {
	addCookie(res, name, value, maxAgeInSeconds, "/");
    }

    /**
     * Remove Cookie with path = "/".
     */
    public static void removeCookie(HttpServletResponse res, String name) {
	addCookie(res, name, null, 0, "/");
    }

    /**
     * Remove Cookie.
     */
    public static void removeCookie(HttpServletResponse res, String name,
	    String path) {
	addCookie(res, name, null, 0, path);
    }

    /**
     * 判断请求类型是否为AJAX请求
     * 
     * @param request
     * @return
     * 
     * 
     */
    public static boolean isAJAXRequest(final HttpServletRequest request) {
	if (null == request) {
	    return false;
	}
	return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
    }
}
